package org.fto.jthink.ai.example.fg;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
import java.util.*;

import org.fto.jthink.util.NumberHelper;
import org.fto.jthink.util.StringHelper;

/**
 * 一个猜拳游戏(a finger-guessing game)
 * 
 * 角色：电脑，玩家
 * 出拳：石头，剪子，布,分别用1,2,3表式
 * Giant
 * @author wenjian(QQ:32312569(32312569@qq.com),mail:try.jiwen@gmail.com)
 */
public class FingerGuessingGame {
  Statistics statistics=null;
  public FingerGuessingGame(){
    statistics = new Statistics();
    PlayerActor pactor = new PlayerActor();
    AIActor aiactor = new AIActor(statistics);
    try {
      help();
      while(true){
        try {
          String phand = pactor.getHand();
          String aihand = aiactor.getHand();
          
          /* 统计比赛结果 */
          int pkresult=statistics.stat(phand, aihand);//返回比赛结果
          prompt(phand, aihand, pkresult);//打印输出
          
          /* 如果电脑连续n次胜率小于60%,对电脑进行重新训练 */
          double lastWinRatio = statistics.statAIActorWinRatio(12);
          System.out.println("电脑最近12次的胜率："+(int)(lastWinRatio*100)+"%");
          if(lastWinRatio<0.70 && Status.useAI){
            aiactor.learning();
          }
          
        } catch (Exception e) {
          doCommand(e.getMessage());
          //e.printStackTrace();
        }        
      }
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
  
  /**
   * 执行命令
   * @param command
   */
  private void doCommand(String command) {
    if(command.equals("q") || command.equals("Q")){
      System.out.println("猜拳游戏结束");
      exit();
    }else if(command.equals("h") || command.equals("H")){
      help();
    }else if(command.toLowerCase().equals("ai")){
      Status.useAI=true;
    }else if(command.toLowerCase().equals("rdm")){
      Status.useAI=false;
    }else{
      //System.out.println(command);
      System.out.println("错误的出拳!");
    }
    
  }

  public static void main(String[] args) {
    new FingerGuessingGame();
  } 

  public void prompt(String phand, String aihand, int result){
    //statistics
    String prompt = promptTemp;
    prompt = StringHelper.replace(prompt, "${phand}", getText(phand));
    prompt = StringHelper.replace(prompt, "${aihand}", getText(aihand));
    prompt = StringHelper.replace(prompt, "${resultPrompt}", resultPrompt(result));
    prompt = StringHelper.replace(prompt, "${aiactorWinNums}", String.valueOf(statistics.getAIActorWinNums()));
    prompt = StringHelper.replace(prompt, "${aiactorWinRatio}", NumberHelper.formatNumber(statistics.getAIActorWinRatio()*100,NumberHelper.NUMBER_IN)+"%");
    prompt = StringHelper.replace(prompt, "${pactorWinNums}", String.valueOf(statistics.getPlayerActorWinNums()));
    prompt = StringHelper.replace(prompt, "${pactorWinRatio}", NumberHelper.formatNumber(statistics.getPlayerActorWinRatio()*100,NumberHelper.NUMBER_IN)+"%");
    prompt = StringHelper.replace(prompt, "${deuceNums}", String.valueOf(statistics.getDeuceNums()));
    prompt = StringHelper.replace(prompt, "${deuceRatio}", NumberHelper.formatNumber(statistics.getDeuceRatio()*100,NumberHelper.NUMBER_IN)+"%");
    prompt = StringHelper.replace(prompt, "${total}", String.valueOf(statistics.getHands().size()));
    System.out.println(prompt);
    System.out.println("-------------------------------------------------------------------------------------------------");
    //System.out.println("你的出拳:("+getText(phand)+"),电脑出拳:("+getText(aihand)+"),"+resultPrompt(result));
  }
  
  private static String promptTemp="你的出拳:(${phand}),电脑出拳:(${aihand}),${resultPrompt},比赛次数:(${total})，电脑胜利次数:(${aiactorWinNums})胜率:(${aiactorWinRatio}),你胜利次数:(${pactorWinNums})胜率:(${pactorWinRatio}),平手次数:(${deuceNums})平率:(${deuceRatio})";
  
  /**
   * 检查是否电脑AI赢了
   */
  public String resultPrompt(int result){
    if(result==1){
      return "你(赢)了";
    }
    if(result==-1){
      return "你(输)了";
    }
    if(result==0){
      return "打个平手";
    }
    return "";
  }

  public String getText(String value){
    if(value.equals("1")){
      return "石头 1";
    }
    if(value.equals("2")){
      return "剪子 2";
    }
    if(value.equals("3")){
      return "布 3";
    }
    return "";
  }
  public void exit(){
    System.exit(0);
  }
  public void help(){
    System.out.println("====================================");
    System.out.println("1,2,3  出拳,分别表示(石头，剪子，布)");
    System.out.println("h      帮助");
   // System.out.println("w<n>   保存(可以保存5档记录,w1,w2,w3,w4,w5)");
   // System.out.println("r<n>   读取(可以读取5档记录,r1,r2,r3,r4,r5)");
    System.out.println("ai     让电脑角色使用人工智能(AI)");
    System.out.println("rdm    让电脑角色随机出拳");
    System.out.println("q      退出");
    System.out.println("====================================");
  }
  
  

   
}

